package com.seocoo.worker.friend;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.seocoo.worker.R;
import com.seocoo.worker.base.http.BaseRsp;
import com.seocoo.worker.base.refresh.SwipeRefreshMoreLayout;
import com.seocoo.worker.friend.adapter.FriendEvaluateAdapter;
import com.seocoo.worker.friend.entity.RxEvaluateSuccess;
import com.seocoo.worker.friend.module.FriendEvaluateModule;
import com.seocoo.worker.friend.req.ReqFriendEvaluate;
import com.seocoo.worker.order.entity.RspOrderIWorkmateInfo;
import com.seocoo.worker.order.model.OrderRelevanceWorkmateModel;
import com.sfh.lib.event.BusEventManager;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.MVVMActivity;

import java.util.List;

/**
 * author : Ssp
 * e-mail : 981297079@qq.com
 * date   : 2020/3/922:50
 * desc   : 评价工友
 * version: 1.0
 */
public class FriendEvaluateActivity extends MVVMActivity {

    private FriendEvaluateAdapter mAdapter = new FriendEvaluateAdapter();
    private TextView tv_commit;
    private String installId;
    private String orderId;
    private SwipeRefreshMoreLayout swipe_refresh_more_layout;

    public static void start(Context context, String installId, String orderId) {
        Intent intent = new Intent(context, FriendEvaluateActivity.class);
        intent.putExtra("installId", installId);
        intent.putExtra("orderId", orderId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friend_evaluate_activity);

        installId = getIntent().getStringExtra("installId");
        orderId = getIntent().getStringExtra("orderId");
        swipe_refresh_more_layout = findViewById(R.id.swipe_refresh_more_layout);
        swipe_refresh_more_layout.setAdapter(mAdapter, true);
        swipe_refresh_more_layout.setEmptyView(R.layout.evaluate_works_empty_view);

        tv_commit = findViewById(R.id.tv_commit);
        tv_commit.setOnClickListener(v -> {
            ReqFriendEvaluate reqFriendEvaluate = new ReqFriendEvaluate();
            reqFriendEvaluate.setAppraiseRespList(mAdapter.getAllEvaluate(orderId));
            getViewModel(FriendEvaluateModule.class).evaluateFriend(reqFriendEvaluate);
        });

        getViewModel(OrderRelevanceWorkmateModel.class).queryWorker(installId);
    }

    @LiveDataMatch
    public void onFriendList(BaseRsp<List<RspOrderIWorkmateInfo>> data) {
        if (data == null) {
            tv_commit.setVisibility(View.GONE);
        } else if (data.getData() == null) {
            tv_commit.setVisibility(View.GONE);
        } else if (data.getData().size() <= 0) {
            tv_commit.setVisibility(View.GONE);
        } else {
            tv_commit.setVisibility(View.VISIBLE);
        }
        swipe_refresh_more_layout.onRefreshCompale(data.getData(), false);
        swipe_refresh_more_layout.onLoadAllCompale();
    }

    @LiveDataMatch
    public void onEvaluateSuccess() {
        RxEvaluateSuccess rxEvaluateSuccess = new RxEvaluateSuccess();
        rxEvaluateSuccess.setSuccess(true);
        BusEventManager.postEvent(rxEvaluateSuccess);
        finish();
    }

}
